package com.itheima.web;

import com.itheima.dto.OrderDTO;
import com.itheima.service.OrderService;
import com.itheima.support.Result;
import com.itheima.utils.RedisUtil;
import com.itheima.vo.OrderVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
public class OrderController {

    @DubboReference
    private OrderService orderService;

    @Autowired
    private AmqpTemplate amqpTemplate;

    //发消息，生成者，消费者在Service里面
    @GetMapping("/sendSms/{telephone}")
    public Result<Boolean>sendSms(@PathVariable("telephone") String telephone){

        //void convertAndSend(String exchange, String routingKey, Object message) throws AmqpException;
        amqpTemplate.convertAndSend("sms_exchange","sms",telephone);
        return Result.build(true);
    }

    @PostMapping("/add")
    public Result<Long>add(@RequestBody OrderVO orderVO){
        return Result.build(orderService.add(orderVO));
    }

    @GetMapping("/successOrder/{orderId}")
    public Result<OrderDTO>successOrder(@PathVariable("orderId") Long orderId){
        return Result.build(orderService.successOrder(orderId));
    }

    //获取redis中对应手机号验证码，防止恶意获取短信验证码
    @GetMapping("/findMsgByTel/{telephone}")
    public Result<String> findMsgByTel(@PathVariable("telephone") String telephone){
        String str = RedisUtil.get("sms:" + telephone);
        return Result.build(str);
    }

}
